﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WSNs
{
    public partial class CtlNetworkConfig : UserControl
    {

        public Func<NetworkConfigStructure,bool> ConfigEvent;
        public Action<NetworkConfigStructure> DisConnectEvent;
        private NetworkConfigStructure _netConfig = new NetworkConfigStructure();
        public CtlNetworkConfig()
        {
            InitializeComponent();
        }

        private void cb_sport_DropDown(object sender, EventArgs e)
        {
            RefreshSerialPort();
        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            Visible = false;
        }

        private void btn_connect_Click(object sender, EventArgs e)
        {
            if (btn_connect.Text == "连   接")
            {
                try
                {
                    _netConfig.SPort = cb_sport.Text;
                    _netConfig.SerialRate = int.Parse(cb_rate.Text);
                    _netConfig.Bo = byte.Parse(txt_bo.Text);
                    _netConfig.So = byte.Parse(txt_so.Text);
                    _netConfig.Lm = byte.Parse(txt_lm.Text);
                    _netConfig.Tm = byte.Parse(txt_Tm.Text);
                    _netConfig.MaxRouter = int.Parse(cb_MaxRouter.SelectedItem.ToString());
                }
                catch (Exception exception)
                {
                    MessageBox.Show(exception.ToString());
                    return;
                }
                if (!ConfigEvent(_netConfig)) return;
                cb_sport.Enabled = false;
                cb_rate.Enabled = false;
                btn_connect.Text = "断开连接";
            }
            else
            {
                try
                {
                    _netConfig.SPort = cb_sport.Text;
                    _netConfig.SerialRate = int.Parse(cb_rate.Text);
                }
                catch (Exception exception)
                {
                    MessageBox.Show(exception.ToString());
                    return;
                }
                DisConnectEvent(_netConfig);
                cb_sport.Enabled = true;
                cb_rate.Enabled = true;
                btn_connect.Text = "连   接";
            }
        }

        private void CtlNetworkConfig_Load(object sender, EventArgs e)
        {
            cb_rate.SelectedIndex = 9;
            RefreshSerialPort();
            cb_MaxRouter.SelectedIndex = 0;
        }

        private void RefreshSerialPort()
        {
            cb_sport.Items.Clear();
            string[] portNames = SerialPort.GetPortNames();
            foreach (string port in portNames)
            {
                cb_sport.Items.Add(port);
            }
            try
            {
                cb_sport.SelectedIndex = cb_sport.Items.Count - 1;
            }
            catch
            {
                
            }
        }

        #region 组网类型切换

        private void rb_single_CheckedChanged(object sender, EventArgs e)
        {
            if (!rb_single.Checked) return;
            txt_bo.Enabled = false;
            txt_so.Enabled = false;
            //txt_cm.Enabled = false;
            txt_lm.Enabled = false;
            txt_bo.Text = "5";
            txt_so.Text = "5";
            txt_lm.Text = "1";
            cb_MaxRouter.SelectedIndex = 0;
            cb_MaxRouter.Enabled = false;
        }

        private void rb_multi_CheckedChanged(object sender, EventArgs e)
        {
            if (rb_multi.Checked != true) return;
            txt_bo.Enabled = true;
            txt_so.Enabled = true;
            //txt_cm.Enabled = false;
            txt_lm.Enabled = true;
            txt_bo.Text = "5";
            txt_so.Text = "2";
            txt_lm.Text = "3";
            cb_MaxRouter.SelectedIndex = 1;
            cb_MaxRouter.Enabled = true;
        }

        private void cb_MaxRouter_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (cb_MaxRouter.SelectedIndex)
            {
                case 0: //单跳
                    txt_bo.Text = "5";
                    txt_so.Text = "5";
                    txt_lm.Text = "1";
                    break;
                case 1: //最多8个
                    txt_bo.Text = "5";
                    txt_so.Text = "2";
                    txt_lm.Text = "3";
                    break;
                case 2: //最多16个
                    txt_bo.Text = "5";
                    txt_so.Text = "1";
                    txt_lm.Text = "3";
                    break;
                case 3: //最多32个
                    txt_bo.Text = "6";
                    txt_so.Text = "2";
                    txt_lm.Text = "3";
                    break;
                case 4: //最多64个
                    txt_bo.Text = "6";
                    txt_so.Text = "1";
                    txt_lm.Text = "3";
                    break;
            }
        }

        #endregion

        /// <summary>
        /// 获取组网参数，依次为：组网标识，信标周期，超帧周期，组网深度，启动延时时间
        /// </summary>
        /// <returns></returns>
        public List<byte> NetworkBuildToList()
        {
            List<byte> lb = new List<byte>();
            lb.Add((byte)SerialPackType.NetworkBuildPack);
            lb.Add(_netConfig.Bo);
            lb.Add(_netConfig.So);
            lb.Add(_netConfig.Lm);
            lb.Add(_netConfig.Tm);
            return lb;
        }
    }
}
